﻿using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Abp.Domain.Entities;
using KelonProud.Extensions;

namespace KelonProud.TableManagement.Domain
{
    public enum DataBaseType:byte
    {
        [EnumDisplay("SqlServer")]
        SqlServer=0,
        [EnumDisplay("Oracle")]
        Oracle = 1,
        [EnumDisplay("MySql")]
        MySql = 2,
        //[EnumDisplay("Db2")]
        //Db2 = 3,
        [EnumDisplay("Access")]
        Access =4,
        [EnumDisplay("Sqlite")]
        Sqlite =5
    }

    [Table("KpTableDataBases")]
    public class KelonDataBase:Entity
    {
        [Column("Name"),StringLength(100)]
        public virtual string Name { get; set; }

        [Column("Type"), StringLength(100)]
        public virtual DataBaseType Type { get; set; }

        [Column("ConnectStr"), StringLength(100)]
        public virtual string ConnectStr { get; set; }

        [Column("ConfigJson"), StringLength(100)]
        public virtual string ConfigJson { get; set; }

        [NotMapped]
        public DataBaseConfigModel Config => ConfigJson.JsonToEntity<DataBaseConfigModel>();
    }

    public class DataBaseConfigModel
    {
        public string Name { get; set; }

        public string User { get; set; }

        public string Ip { get; set; }

        public string Port { get; set; }

        public string PassWord { get; set; }

        public string CreateConnectStr(DataBaseType type)
        {
            var result = "";

            switch (type)
            {
                case DataBaseType.MySql:
                    result = "Data Source={0};port={1};Initial Catalog={2};uid={3};password={4};Charset=utf8;Allow User Variables=true".FormatWith(Ip,Port,Name,User,PassWord);
                    break;
            }

            return result;
        }
    }
}
